package run.siyuan.manag.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import run.siyuan.common.constant.Constant;
import run.siyuan.manag.entity.SysMenu;
import run.siyuan.manag.entity.SysRole;
import run.siyuan.manag.mapper.SysMenuMapper;
import run.siyuan.manag.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import run.siyuan.manag.service.SysRoleService;

import java.util.List;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author siyuan
 * @since 2021-01-18
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<SysMenu> findByUser(String username) {
        if (StrUtil.isBlank(username) || StrUtil.equalsIgnoreCase(Constant.ADMIN,username)){
            return baseMapper.selectList(null);
        }
        // 根据用户查询就是
        SysRole sysRole = sysRoleService.getSysRoleByUserName(username);
        if (ObjectUtil.isEmpty(sysRole)){
            log.info("用户:{} 角色为空",username);
            return null;
        }
        // 根据角色查询菜单
        return baseMapper.findByUserName(sysRole.getId());
    }
}
